package com.my.feign.config.fallBack;

import feign.Target;
import feign.hystrix.FallbackFactory;
import lombok.AllArgsConstructor;
import org.springframework.cglib.proxy.Enhancer;

/**
 * 自定义全局熔断FallbackFactory实现类。
 */
@AllArgsConstructor
public class GlobalFallbackFactory<T> implements FallbackFactory<T> {

    /* 被代理目标对象 */
    private Target<T> target;

    @Override
    public T create(Throwable cause) {
        // 获取@FeignClient注解的接口类型
        Class<T> targetType = target.type();
        String targetName = target.name();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetType);
        enhancer.setUseCache(true);
        enhancer.setCallback(
                new XcloudFeignFallback<>(targetType, targetName, cause)
        );
        return (T) enhancer.create();
    }
}

